package com.team.hiring.talent.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.team.hiring.talent.dao.ResumeDao;
import com.team.hiring.talent.entity.ResumeEntity;
import com.team.hiring.talent.entity.TalentEntity;

/**
 * 简历的管理
 * Servlet implementation class ResumeServlet
 */
public class ResumeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ResumeDao resumeDao = new ResumeDao();
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//解决post请求的乱码问题
		request.setCharacterEncoding("UTF-8"); 
		String command = request.getParameter("command");
		if("jumptoaddResume".equals(command)){
			jumptoaddResume(request,response);
		}else if("addResume".equals(command)){
			addResume(request,response);
		}else if("selectResume".equals(command)){
			selectResume(request,response);
		}else if("toUpdate".equals(command)){
			toUpdate(request,response);
		}else if("updateResume".equals(command)){
			updateResume(request,response);
		}else if("delete".equals(command)){
			delete(request,response);
		}else if("homepageSelectResume".equals(command)){
			homepageSelectResume(request,response);
		}
		else{
			System.out.println("请求非法!");
		}
		
		
	}
 
	/**
	 * 如果talentEntity不为空，则查找简历信息
	 * @param request
	 * @param response
	 */
	private void homepageSelectResume(HttpServletRequest request,
			HttpServletResponse response) {
		int talentId = Integer.parseInt(request.getParameter("talentId"));
		List<ResumeEntity> resumeEntityList = resumeDao.homepageSelectResume(talentId);
		request.setAttribute("resumeEntityList",resumeEntityList);
		try {
			request.getRequestDispatcher("/WEB-INF/view/common/positionDetailInfo.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

/**
 * 查询简历信息
 * 
 * @param request
 * @param response
 * @throws IOException 
 * @throws ServletException 
 */
private void selectResume(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
	TalentEntity talentEntity = (TalentEntity) request.getSession().getAttribute("talentEntity");
	int talentId = talentEntity.getId();
	List<ResumeEntity> resumeList = resumeDao.resumeList(talentId);
	if(resumeList!=null){
	request.getSession().setAttribute("resumeList", resumeList);
	request.getRequestDispatcher("ApplicationServlet?command=queryApplication&&talentId="+talentEntity.getId()).forward(request, response);
	}
	}

	/**
	 * 跳转到简历新增页面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void jumptoaddResume(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/view/talent/resume-add.jsp").forward(request, response);
	}
	
	/**
	 * 新增简历
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void addResume(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		//1、获取前台参数
		int talentId = Integer.parseInt(request.getParameter("talentId"));
		String intention = request.getParameter("intention");
		String workExperience = request.getParameter("workExperience");
		String projectExperience = request.getParameter("projectExperience");
		//2、调用业务逻辑
		ResumeEntity resumeEntity = new ResumeEntity();
		resumeEntity.setTalentId(talentId);
		resumeEntity.setIntention(intention);
		resumeEntity.setWorkExperience(workExperience);
		resumeEntity.setProjectExperience(projectExperience);
		int row = resumeDao.add(resumeEntity);
		//3、转向页面
		if(row!=0){
			request.getRequestDispatcher("ResumeServlet?command=selectResume").forward(request, response);
		}else{
			System.out.println("新增失败");
		}
	}
	
	/**
	 * 跳转到简历修改页面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void toUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		ResumeEntity resume = resumeDao.selectById(id);
		request.getSession().setAttribute("resume", resume);
		request.getRequestDispatcher("WEB-INF/view/talent/resume-update.jsp").forward(request, response);
	}

	
	/**
 	 * 简历修改
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void updateResume(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//1、获取前台参数
		        int id = Integer.parseInt(request.getParameter("id"));
				int talentId = Integer.parseInt(request.getParameter("talentId"));
				String intention = request.getParameter("intention");
				String workExperience = request.getParameter("workExperience");
				String projectExperience = request.getParameter("projectExperience");
				
				
				//2、调用业务逻辑
				ResumeEntity resumeEntity = new ResumeEntity();
				resumeEntity.setId(id);
				resumeEntity.setTalentId(talentId);
				resumeEntity.setIntention(intention);
				resumeEntity.setWorkExperience(workExperience);
				resumeEntity.setProjectExperience(projectExperience);
				int row = resumeDao.update(resumeEntity);
				//3、转向页面
				if(row == 0){
					System.out.println("简历修改失败");
				}else{
					request.getRequestDispatcher("ResumeServlet?command=selectResume").forward(request, response);
				}
	}
	
	
	/**
	 * 删除简历
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//1、获取前台参数
		int id = Integer.parseInt(request.getParameter("id"));
		//2、调用业务逻辑
		int row = resumeDao.delete(id);
		
		//3、转向页面
		if(row == 0){
			System.out.println("简历删除失败");
		}else{
			request.getRequestDispatcher("ResumeServlet?command=selectResume").forward(request, response);
		}
	}


	
	

}
